﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.HealthyWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.HealthyWay
{
    public class DelHealthyCommandHandler : IRequestHandler<DelHealthyCommand, ApiResult<int>>
    {
        IBaseReponsitory<Healthys> Healthys;

        public DelHealthyCommandHandler(IBaseReponsitory<Healthys> Healthys)
        {
            this.Healthys = Healthys;
        }

        public async Task<ApiResult<int>> Handle(DelHealthyCommand request, CancellationToken cancellationToken)
        {
            var list =await Healthys.GetQuery().FirstOrDefaultAsync(c => c.Rabit_Batch == request.Batch&&c.Healthys_Id == request.id && !c.IsDel );
            list.IsDel = true;
            await Healthys.UpdateAsync(list);
            return new ApiResult<int>
            {
                Code = ApiEnum.Success,
                Message = "删除成功",
                Data = 1
            };
        }
    }
}
